/**
 * 
 */
package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.gameserver.model.ConsignmentItem;
import labox.innovation.gameserver.model.ConsignmentManager;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.itemcontainer.ConsignmentWarehouse;
import labox.innovation.gameserver.model.itemcontainer.PcInventory;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.BuyConsItemResultPacket;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.templates.item.FuzeEconomicManager.OPSTATE;

/**
 * @author ljsh
 * @date 2010-5-5
 * @version 1.0
 * @description  购买寄售物品
 */
public final class BuyItemConsignment extends L2GameClientPacket {
	private static Logger _log=LoggerFactory.getLogger(RequestItemShelve.class);
	private static final String _C_BUYITEMCONSIGNMENT = "_C_BuyItemConsignment";

	private long itemId;

	private static final String optionName = "购买寄售物品";

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.network.clientpackets.L2GameClientPacket#getType()
	 */
	@Override
	public String getType() {
		return _C_BUYITEMCONSIGNMENT;
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.network.clientpackets.L2GameClientPacket#readImpl()
	 */
	@Override
	protected void readImpl() {
		itemId = readLong();
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.network.clientpackets.L2GameClientPacket#runImpl()
	 */
	@Override
	protected void runImpl() {
		FuzePcInstance active = getClient().getActiveChar();

		if (active == null)
			return;
		ConsignmentItem conitem = ConsignmentManager.getConsignmentItem(itemId);
		if (conitem == null || !conitem.isShelve() || conitem.getItem() == null) {
			active.sendPacket(new SystemMessage(SystemMessageId.BUY_CONS_ITEM_SUCCESS));
			active.sendPacket(new SystemMessage(SystemMessageId.CONS_ITEM_SHELF_OR_SELLED));
			return;
		}
		int _priceType = conitem.getPriceType();
		int money = 0;
		
		if (_priceType == PcInventory.MONEY_NOTE_ID) {
			money = active.getInventory().getMoneyNote();
		}
//		if (_priceType == PcInventory.MONEY_YAUNBAO_ID) {
//			money = active.getInventory().getMoneyYuanBao();
//		}
		int buyItemHandleFee=ConsignmentManager.calculateBuyConsignmentHandleFee(conitem.getPrice());
		if (money < conitem.getPrice()+buyItemHandleFee) {
			active.sendPacket(new SystemMessage(SystemMessageId.NO_ENOUGH_MONEY_TO_BUY_ITEM));
			return;
		}
		if (!active.getInventory().validateCapacity(conitem.getItem())) {
			active.sendPacket(new SystemMessage(SystemMessageId.INVENTORY_IS_FULL));
			return;
		}
		boolean isOk = ConsignmentManager.shelf(conitem);//从上架列表中移除
		if (isOk) {
//			if (_priceType == PcInventory.MONEY_SLIVER_ID) {
//				active.getInventory().reduceMoneySilver(optionName, (int) conitem.getPrice(), active, null);
//			}
			if (_priceType == PcInventory.MONEY_NOTE_ID) {
				active.reduceMoneyNote(OPSTATE.TRADE.getValue(), conitem.getPrice()+buyItemHandleFee, null, true);
			}
//			else if (_priceType == PcInventory.MONEY_YAUNBAO_ID) {
//				active.reduceMoneyYuanBao(OPSTATE.TRADE.getValue(),conitem.getPrice(), null, true);
//			}
			long ownerid = conitem.getItem().getOwnerId();
			ConsignmentWarehouse house = ConsignmentManager.getConsignmentWarehouse(ownerid);
			StringBuilder build=new StringBuilder();
			house.addMoney(_priceType, conitem.getPrice());//给卖方增加金钱
			_log.info(build.append("卖出寄售物品:得到(").append(_priceType).append("),").append(conitem.getPrice()).toString(), ownerid, ownerid);
			active.addItem(optionName, conitem.getItem(), null, true);//给买方添加道具
			build.setLength(0);
			_log.info(build.append("买入寄售物品:得到").append(conitem.getItem().getName()).append("(").append(conitem.getItem().getObjectId()).append("),").append(conitem.getItem().getCount()).toString(), active.getObjectId(), active.getName());
			house.removeConsignmentItem(conitem);//从卖家寄售列表中删除物品
			build.setLength(0);
			_log.info(build.append("寄售物品被卖出:").append(conitem.getItem().getName()).append("(").append(conitem.getItem().getObjectId()).append("),").append(conitem.getItem().getCount()).toString(), active.getObjectId(), active.getName());
			build.setLength(0);
			_log.info(build.append("寄售交易:").append(conitem.getItem().getName()).append("(").append(conitem.getItem().getObjectId()).append("),").append(conitem.getItem().getCount()).append("从").append(ownerid).append("到").append(active.getObjectId()).toString(), conitem.getItem().getObjectId(), conitem.getItem().getName());
			active.sendPacket(new BuyConsItemResultPacket(conitem.getItemId()));
			active.sendPacket(new SystemMessage(SystemMessageId.BUY_CONS_ITEM_SUCCESS));//发送上架成功消息
			house.addRecord(house.createSalesRecord(conitem.getItem().getItemName(), conitem.getItem().getCount(), money, _priceType));
		}
	}

}
